package com.timothy.junk.calculate;

import com.sina.stock.SinaStockClient;
import com.sina.stock.SinaStockInfo;
import com.timothy.junk.cbond.bean.CBond;
import com.timothy.junk.cbond.bean.CBondResult;

import java.util.List;
import java.util.ArrayList;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA. User: qtang Date: Jul 5, 2013 Time: 9:29:58 AM To
 * change this template use File | Settings | File Templates.
 */
public class CBondCalculator {

	// get static values
	List<CBond> cBondList = new ArrayList<CBond>();
	List<CBondResult> cBondResultList = new ArrayList<CBondResult>();
	int count;

	public CBondCalculator(List<CBond> cBondList) {
		this.cBondList = cBondList;
		count = cBondList.size();
	}

	// input data
	String[] cBondCodes;
	String[] stockCodes;

	public void initial() {
		cBondCodes = new String[count];
		stockCodes = new String[count];

		int i = 0;
		for (CBond cBond : cBondList) {
			cBondCodes[i] = cBond.getBond_code();
			stockCodes[i] = cBond.getStock_code();
			i++;
		}
	}

	// data by SinaStockClient
	List<SinaStockInfo> cBondStockList;
	List<SinaStockInfo> cStockList;

	public void getStockData(SinaStockClient sClient) {
		try {
			cBondStockList = sClient.getStockInfoList(cBondCodes);
			cStockList = sClient.getStockInfoList(stockCodes);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (SinaStockInfo.ParseStockInfoException e) {
			e.printStackTrace();
		}
	}

	public void calculate() {
		int i = 0;
		for (CBond cBond : cBondList) {
			SinaStockInfo cBondStock = cBondStockList.get(i);
			SinaStockInfo cStock = cStockList.get(i);

			CBondResult cBondResult = new CBondResult();
			cBondResult.setIndex(cBond.getIndex());
			cBondResult.setcBondName(cBond.getBond_name());
			cBondResult.setcBondPrice(cBondStock.getNowPrice());
			double bondValue = (cStock.getNowPrice() / cBond.getTransPrice()) * 100;
			cBondResult.setcBondValue(bondValue);
			double discount = (bondValue - cBondStock.getNowPrice())/ cBondStock.getNowPrice();
			cBondResult.setDiscount(discount);

			cBondResultList.add(cBondResult);
			i++;
		}
	}

	public List<CBondResult> getcBondResultList() {
		return cBondResultList;
	}

}
